package com.wakala.employment.fragment.other;

import android.os.Handler;
import android.os.Looper;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.github.gzuliyujiang.wheelpicker.OptionPicker;
import com.github.gzuliyujiang.wheelpicker.contract.OnOptionPickedListener;
import com.github.gzuliyujiang.wheelpicker.contract.OnOptionSelectedListener;
import com.github.gzuliyujiang.wheelpicker.widget.OptionWheelLayout;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.core.CenterPopupView;
import com.wakala.employment.R;
import com.wakala.employment.adapter.bean.OptionBean;
import com.wakala.employment.core.BaseFragment;
import com.wakala.employment.databinding.FragmentResumeExigenceBinding;
import com.xuexiang.xpage.annotation.Page;
import com.xuexiang.xpage.enums.CoreAnim;
import com.xuexiang.xui.widget.actionbar.TitleBar;
import com.xuexiang.xutil.data.DateUtils;

import java.text.DateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link ResumeExigenceFragment#} factory method to
 * create an instance of this fragment.
 */
@Page(anim = CoreAnim.none)
public class ResumeExigenceFragment extends BaseFragment<FragmentResumeExigenceBinding> implements View.OnClickListener{

    private static String TAG = ResumeExigenceFragment.class.getSimpleName();

    private Handler mHandler = new Handler(Looper.getMainLooper());

    private Integer mDays = 1;


    @NonNull
    @Override
    protected FragmentResumeExigenceBinding viewBindingInflate(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, boolean attachToRoot) {
        return FragmentResumeExigenceBinding.inflate(inflater, container, attachToRoot);
    }

    @Override
    protected TitleBar initTitle() {
        return super.initTitle().setTitle("");
    }


    @Override
    protected void initViews() {
        //加急时间
        binding.tvExigenceTime.setText("24小时(1天)");
        //消耗积分
        binding.tvExigenceIntegral.setText("12");
        //到期时间
        binding.tvExigenceTimeEnd.setText(DateUtils.date2String(DateUtils.nDaysAfterToday(1), DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.SHORT)));
    }

    @Override
    protected void initListeners() {
        super.initListeners();
        binding.rlChooseTime.setOnClickListener(this);
        binding.btnExigenceConfirm.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if(id== R.id.rl_choose_time){
            showChooseTime();
        } else if (id ==R.id.btn_exigence_confirm) {
            new XPopup.Builder(getContext()).asCustom(new CenterPopupView(requireContext()) {
                @Override
                protected int getImplLayoutId() {
                    return R.layout.dialog_alter;
                }

                @Override
                protected void onCreate() {
                    super.onCreate();
                    findViewById(R.id.btn_confirm).setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            dismiss();
                        }
                    });
                    TextView title = findViewById(R.id.txt_dialog_content);
                    title.setText("尊敬的用户，河狸帮给新用户赠送免费体验会员功能权益，体验期间，您可享受免费加急服务，您的加急请求已经提交给客服审核。");
                }
            }).show();
        }
    }

    private void showChooseTime() {
        List<OptionBean> data = new ArrayList<>();
        data.add(new OptionBean("1", "24小时(1天)"));
        data.add(new OptionBean("2", "48小时(2天)"));
        data.add(new OptionBean("3", "72小时(3天)"));
        data.add(new OptionBean("4", "4天"));
        data.add(new OptionBean("5", "5天"));
        data.add(new OptionBean("6", "6天"));
        data.add(new OptionBean("7", "7天"));
        data.add(new OptionBean("8", "8天"));
        data.add(new OptionBean("9", "9天"));
        data.add(new OptionBean("10", "10天"));
        data.add(new OptionBean("15", "15天"));
        data.add(new OptionBean("30", "30天"));

        OptionPicker picker = new OptionPicker(getActivity());
        picker.setTitle("加急时间");
        picker.setBodyWidth(140);
        picker.setData(data);
        picker.setDefaultPosition(0);
        picker.setOnOptionPickedListener(new OnOptionPickedListener() {
            @Override
            public void onOptionPicked(int position, Object item) {
                OptionBean optionBean = data.get(position);
                mDays = Integer.parseInt(optionBean.getId());
                binding.tvExigenceTime.setText(item.toString());
                binding.tvExigenceIntegral.setText(String.valueOf(Integer.parseInt(optionBean.getId())*12));
                binding.tvExigenceTimeEnd.setText(DateUtils.date2String(DateUtils.nDaysAfterToday(Integer.parseInt(optionBean.getId())), DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.SHORT)));
            }
        });
        OptionWheelLayout wheelLayout = picker.getWheelLayout();
        wheelLayout.setSelectedTextBold(true);
        wheelLayout.setOnOptionSelectedListener(new OnOptionSelectedListener() {
            @Override
            public void onOptionSelected(int position, Object item) {
                picker.getTitleView().setText(picker.getWheelView().formatItem(position));
            }
        });
        picker.show();
    }
}